package com.gongpingjialoan.adapter.car;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.LikeCarActivity;
import com.gongpingjialoan.bean.CarSource;
import com.gongpingjialoan.imageload.ImageLoad;
import com.gongpingjialoan.utility.BaseViewHolder;
import com.gongpingjialoan.utility.Utils;

import java.util.List;

/**
 * Created by Administrator on 2015/10/27.
 */
public class LikeCarAdapter  extends BaseAdapter {

    private List<CarSource>  carSources;

    private Context context;

    public LikeCarAdapter(List<CarSource> carSources, Context context) {
        this.carSources = carSources;
        this.context = context;
    }

    @Override
    public int getCount() {
        return carSources.size();
    }

    @Override
    public Object getItem(int position) {
        return carSources.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.like_car_item, parent , false);
        }
        TextView titleTextView = BaseViewHolder.get(convertView, R.id.title);
        TextView mileTextView = BaseViewHolder.get(convertView, R.id.mile);
        TextView evalTextView = BaseViewHolder.get(convertView, R.id.eval_time);
        Button btn = BaseViewHolder.get(convertView, R.id.btn);
        ImageView networkImageView = BaseViewHolder.get(convertView, R.id.pic);


        final CarSource carSource = carSources.get(position);
        if(carSource.is_default){
            btn.setOnClickListener(null);
            btn.setText("已设置为默认爱车");
            btn.setBackgroundResource(R.drawable.like_car_defalut_color);
        }else{
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LikeCarActivity likeCarActivity = (LikeCarActivity) context;
                    likeCarActivity.setDefalutCar(carSource.mId);
                }
            });
            btn.setText("设为默认爱车");
            btn.setBackgroundResource(R.drawable.set_like_car_bg);
        }
        titleTextView.setText(carSource.model_zh + carSource.model_detail_zh);
        mileTextView.setText(carSource.mYear+"年上牌/" + carSource.mMile +"万公里");


        evalTextView.setText(Utils.getParseDay("yyyy.MM.dd" , carSource.eval_time) + "添加");


        ImageLoad.LoadImage(networkImageView, carSource.mThumbnail, R.drawable.car_loading, R.drawable.car_no,context);
      //  Glide.with(context).load(carSource.mThumbnail).placeholder(R.drawable.car_loading).into(networkImageView);
        return convertView;
    }
}
